package com.itheima.before_leyou_store.Sku;

import com.alibaba.fastjson.JSON;
import com.itheima.before_leyou_store.pojo.Sku;
import com.itheima.before_leyou_store.service.SkuService;
import com.itheima.before_leyou_store.service.impl.SkuServiceImpl;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class Task1 {
    private RestHighLevelClient client;
    private static final String INDEX_TEMPLATE ="{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"name\":{\n" +
            "        \"type\": \"text\"\n" +
            "        , \"analyzer\": \"ik_max_word\"\n" +
            "      },\n" +
            "      \"price\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"num\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"alertNum\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"image\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"creatTime\":{\n" +
            "        \"type\": \"date\"\n" +
            "      },\n" +
            "      \"updateTime\":{\n" +
            "        \"type\": \"date\"\n" +
            "      },\n" +
            "      \"spuId\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"categoryId\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"categoryName\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"brandName\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"spec\":{\n" +
            "        \"type\": \"text\"\n" +
            "        , \"analyzer\": \"ik_max_word\"\n" +
            "      },\n" +
            "      \"saleNum\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"commentNum\":{\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"status\":{\n" +
            "        \"type\": \"keyword\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
    @Resource
    private SkuService skuService;
    @BeforeEach
    public void setUp() throws Exception {
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.187.128", 9200, "http")));

    }
    @Test
    public void test() throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.187.128", 9200, "http")));
        GetRequest request = new GetRequest("sku","100000003145");
        GetResponse response = client.get(request,RequestOptions.DEFAULT);
        Map<String, Object> source = response.getSource();
        Sku sku = JSON.parseObject(JSON.toJSONString(source), Sku.class);
        System.out.println(sku);
    }

    @Test
    public void testCreateIndex() throws IOException {

        CreateIndexRequest request = new CreateIndexRequest("sku");

        request.source(INDEX_TEMPLATE, XContentType.JSON);

        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);


    }

    @Test
    public void addToElasticsearchFromMysql() throws IOException {
       int total = skuService.findTotal();
       for (int i = 0;i<=total;i=i+5000){
          List<Sku> skus= skuService.findSkus(i);
           BulkRequest request = new BulkRequest();
           for (Sku sku : skus) {
               request.add(new IndexRequest("sku").id(sku.getId()).source(JSON.toJSONString(sku),XContentType.JSON));
           }
          client.bulk(request,RequestOptions.DEFAULT);

       }
    }



    @AfterEach
    public void tearDown() throws Exception {
        client.close();
    }
}
